﻿using EMoney.IStock.Client.Core;
using EMoney.IStock.Client.DataAccess.QQ;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.QQ;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using ThinkDev.Logging;

namespace EMoney.IStock.Client.Business.QQ
{
    /// <summary>
    /// 交易业务处理类
    /// </summary>
    public class TradeBll
    {
        /// <summary>
        /// 获取报名验证码
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="mo"></param>
        public string GetCode(string qq,string mo)
        {
            string ret = string.Empty;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/getCode?yidata=", StringHelper.GetYiData(qq), "&mo=" + mo + "&tp=ym");

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon common = JsonHelper.ToObject<QQCommon>(temp);

                            if (common != null)
                            {
                                ret = common.code;

                                if (common.code != "0")
                                {
                                    ret = string.Concat(common.code, ".", common.msg);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = ex.Message;

                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("获取报名验证码失败.", qq));
            }

            return ret;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="code"></param>
        /// <param name="tel"></param>
        public string RegUser(string qq, string code, string tel)
        {
            string ret = string.Empty;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/regUser?yidata=", StringHelper.GetYiData(qq), "&code=" + code + "&tel=" + tel + "&tp=ym");

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        //{"code":"40051","msg":"\u7535\u8bdd\u9519\u8bef!","data":{"response":"1"}}
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon common = JsonHelper.ToObject<QQCommon>(temp);

                            if (common != null)
                            {
                                ret = common.code;

                                if (ret != "0")
                                {
                                    ret = string.Concat(common.code, ".", common.msg);
                                }
                                else
                                {
                                    TradeDal tradeDal = new TradeDal();

                                    try
                                    {
                                        tradeDal.AddRegInfo(qq, tel);   //新增报名信息

                                        First(qq);  //增加1次抽奖机会

                                        CacheManager.QQCache.Set<string>(string.Concat(QQConstDefine.REG_CACHED_KEY, ":", qq), qq);
                                    }
                                    catch (Exception ex1)
                                    {
                                        LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex1), string.Concat("新增参赛信息失败.", qq));
                                    }                                    
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = ex.Message;

                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("用户注册失败.", qq));
            }

            return ret;
        }

        /// <summary>
        /// 是否参赛用户
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public bool IsPlayer(string qq)
        {
            bool flag = false;

            try
            {
                string key = string.Concat(QQConstDefine.ISPLAYER_CACHED_KEY, ".", qq);

                string result = CacheManager.QQCache.Get<string>(key);

                if (string.IsNullOrEmpty(result))
                {
                    Assets assets = GetAssets(qq);

                    if (assets != null)
                    {
                        CacheManager.QQCache.Set<string>(key, "1");

                        flag = true;
                    }
                }
                else
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("是否参赛用户失败.", qq));
            }

            return flag;
        }

        /// <summary>
        /// 绑定QQ
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="associatedlogin">1是联合登录 0则不是</param>
        /// <param name="gid"></param>
        /// <param name="openid"></param>
        /// <returns></returns>
        public string BindQQ(string userid, string pwd, string associatedlogin, string gid, string openid)
        {
            string ret = string.Empty;

            try
            {
                string qq = GetUinByOpenId(openid,userid);

                if (string.IsNullOrEmpty(qq))
                {
                    ret = "未获取到QQ号码";
                }
                else
                {
                    if (associatedlogin == "1" || pwd == "0")
                    {
                        pwd = "0";
                    }
                    else
                    {
                        pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5").ToLower();
                    }

                    string url = string.Concat(ConfigHelper.WebAPIUrl, "/User/api/IStock.BindMapUserNew?appid=10001&account=",
                userid, "&toaccount=", qq, "&type=100&password=", pwd, "&openid=" + openid);

                    url = WebApiHelper.CreateUrl(url);

                    string result = WebApiHelper.getUrl(url);

                    if (!string.IsNullOrEmpty(result))
                    {
                        WebApiJson jsonObj = JsonHelper.ToObject<WebApiJson>(result);

                        if (jsonObj != null)
                        {
                            ret = jsonObj.RetMsg;

                            if (jsonObj.RetCode == "1")
                            {
                                //记录userid、qq对应关系
                                CacheManager.DefaultCache.Set<string>(string.Concat(QQConstDefine.USERQQ_CACHED_KEY, ".", userid), qq);

                                //清空会员中心缓存
                                CacheManager.DefaultCache.Remove(string.Concat("EMoney.IStock.Client.Business.MC.MyInfoBll.MyInfo.", userid));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = "系统繁忙，请稍后再试";
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("绑定QQ号码失败."));
            }

            return ret;
        }

        /// <summary>
        /// 获取用户资产信息
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public Assets GetAssets(string qq)
        {
            if (string.IsNullOrEmpty(qq))
            {
                return null;
            }

            Assets assets = null;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/getAssets?yidata=",StringHelper.GetYiData(qq), "&tp=ym");

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);
                //string temp = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":{\"cash_assets\":\"965816.00\",\"total_sz\":\"33122.00\",\"total_zc\":\"998986.00\",\"yinkui\":\"-1014.00\",\"cangwei\":\"3.32\",\"rankIndex\":\"5\",\"rankDayIndex\":\"1\",\"rankWeekIndex\":\"2\"}}";

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null && qqcommon.code == "0")
                            {
                                assets = JsonHelper.ToObject<Assets>(temp);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("获取用户资产信息失败.", qq));
            }

            return assets;
        }

        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="qq">QQ号码</param>
        /// <param name="code">股票代码（沪市前缀sh；深市前缀sz）</param>
        /// <param name="buysell">buy：买 sell:卖</param>
        /// <param name="price">价格</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public string AddOrder(string qq, string code, string buysell, decimal price, int count)
        {
            if (string.IsNullOrEmpty(qq))
            {
                return null;
            }

            string ret = string.Empty;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmOrder/addOrder?yidata=", StringHelper.GetYiData(qq), "&code=", code,
                    "&buysell=", buysell, "&price=", price, "&count=", count, "&tp=ym");

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            temp = temp.Replace("\0", "");
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null)
                            {
                                ret = qqcommon.code;

                                if (ret != "0")
                                {
                                    ret = string.Concat(qqcommon.code, ".", qqcommon.msg);
                                }

                                LogManager.TradeLogger.Info(string.Concat("qq:", qq, ".code:", code, ".buysell:", buysell, ".price:", price, ".count:", count,
                                    ".retcode:", qqcommon.code, ".retmsg:", qqcommon.msg));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = ex.Message;

                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("委托失败.", qq));
            }

            return ret;
        }

        /// <summary>
        /// 成交单查询
        /// </summary>
        /// <param name="qq">QQ号码</param>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        public DealOrders GetDealOrders(string qq, string start, string end)
        {
            DealOrders dealOrders = null;

            //string ret = string.Empty;

            //ret = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":[{\"deal_count\":\"100\",\"total\":\"16219.00\",\"c_time\":\"2014-09-26 14:28:08\",\"stock_code\":\"sh600519\",\"trade_direction\":\"buy\",\"stock_name\":\"\u8d35\u5dde\u8305\u53f0\",\"trade_price\":\"163.00\",\"trade_count\":\"100\",\"deal_price\":\"162.19\",\"dtype\":\"deal\"},{\"deal_count\":\"500\",\"total\":\"7500.00\",\"c_time\":\"2014-09-26 13:52:59\",\"stock_code\":\"sz002423\",\"trade_direction\":\"buy\",\"stock_name\":\"\u4e2d\u539f\u7279\u94a2\",\"trade_price\":\"15.00\",\"trade_count\":\"500\",\"deal_price\":\"15.00\",\"dtype\":\"deal\"},{\"deal_count\":\"500\",\"total\":\"7500.00\",\"c_time\":\"2014-09-26 13:50:44\",\"stock_code\":\"sz002423\",\"trade_direction\":\"buy\",\"stock_name\":\"\u4e2d\u539f\u7279\u94a2\",\"trade_price\":\"15.00\",\"trade_count\":\"500\",\"deal_price\":\"15.00\",\"dtype\":\"deal\"}]}";

            //return JsonHelper.ToObject<DealOrders>(ret);

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmOrder/getDealOrders?yidata=", StringHelper.GetYiData(qq),
                    "&start=", start, "&end=", end, "&tp=ym");

                string result = WebApiHelper.getUrl(url);
                //string temp = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":[{\"deal_count\":\"300\",\"total\":\"1446.00\",\"c_time\":\"2014-09-29 14:13:25\",\"stock_code\":\"sh600317\",\"trade_direction\":\"buy\",\"stock_name\":\"\u8425\u53e3\u6e2f\",\"trade_price\":\"4.90\",\"trade_count\":\"300\",\"deal_price\":\"4.82\",\"dtype\":\"deal\"},{\"deal_count\":\"300\",\"total\":\"1446.00\",\"c_time\":\"2014-09-29 14:08:10\",\"stock_code\":\"sh600317\",\"trade_direction\":\"buy\",\"stock_name\":\"\u8425\u53e3\u6e2f\",\"trade_price\":\"4.90\",\"trade_count\":\"300\",\"deal_price\":\"4.82\",\"dtype\":\"deal\"}]}";

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            temp = temp.Replace("\0", "");
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null && qqcommon.code == "0")
                            {
                                dealOrders = JsonHelper.ToObject<DealOrders>(temp);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("成交单查询失败.", qq));
            }

            return dealOrders;
        }

        /// <summary>
        /// 获得用户股票购买信息
        /// </summary>
        /// <param name="qq"></param>
        public Stocks GetStocks(string qq)
        {
            Stocks stocks = null;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/getStocks?yidata=", StringHelper.GetYiData(qq), "&tp=ym");

                string result = WebApiHelper.getUrl(url);
                //string temp = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":[{\"stock_id\":\"5\",\"user_qq\":\"115536016\",\"stock_code\":\"sh600317\",\"stock_count\":\"0\",\"stock_count_today\":\"600\",\"stock_count_freezed\":\"0\",\"stock_cost\":\"4.84\",\"c_time\":\"2014-09-29 14:13:28\",\"stock_name\":\"\u8425\u53e3\u6e2f\",\"yinkui\":\"0%\"},{\"stock_id\":\"2\",\"user_qq\":\"115536016\",\"stock_code\":\"sh600519\",\"stock_count\":\"0\",\"stock_count_today\":\"100\",\"stock_count_freezed\":\"0\",\"stock_cost\":\"162.24\",\"c_time\":\"2014-09-26 14:28:16\",\"stock_name\":\"\u8d35\u5dde\u8305\u53f0\",\"yinkui\":\"0%\"},{\"stock_id\":\"1\",\"user_qq\":\"115536016\",\"stock_code\":\"sz002423\",\"stock_count\":\"0\",\"stock_count_today\":\"1000\",\"stock_count_freezed\":\"0\",\"stock_cost\":\"15.01\",\"c_time\":\"2014-09-26 13:52:52\",\"stock_name\":\"\u4e2d\u539f\u7279\u94a2\",\"yinkui\":\"0%\"}]}";

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            stocks = JsonHelper.ToObject<Stocks>(temp);

                            //if (stocks != null && stocks.data != null && stocks.data.Count > 0)
                            //{
                            //    ret = JsonHelper.ToJson(stocks.data);
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("获得用户股票购买信息失败.", qq));
            }

            return stocks;
        }

        /// <summary>
        /// 获取待撤单信息
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public UnCancelOrder GetUnCancelOrder(string qq)
        {
            if (string.IsNullOrEmpty(qq))
            {
                return null;
            }

            UnCancelOrder unCancelOrder = null;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmOrder/getUnCancelOrder?yidata=", StringHelper.GetYiData(qq), "&buysell=buy&tp=ym");

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);
                //string temp = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":[]}";

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null && qqcommon.code == "0")
                            {
                                unCancelOrder = JsonHelper.ToObject<UnCancelOrder>(temp);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("获取待撤单信息失败.", qq));
            }

            return unCancelOrder;
        }

        /// <summary>
        /// 撤单
        /// </summary>
        /// <param name="qq">QQ号码</param>
        /// <param name="oid">订单号</param>
        /// <param name="code">股票代码</param>
        public string DoCancelOrder(string qq, string oid, string code)
        {
            string ret = string.Empty;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmOrder/doCancelOrder?yidata=", StringHelper.GetYiData(qq), "&oid=", oid, "&code=", code, "&tp=ym");

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);
                //string result = "{\"code\":\"0\",\"msg\":\"OK\",\"data\":[{\"order_id\":\"5\",\"order_uuid\":\"b22360af-b751-5d53-3570-54266ec4b2b6\",\"user_qq\":\"115536016\",\"stock_code\":\"sh600399\",\"stock_name\":\"\u629a\u987a\u7279\u94a2\",\"trade_direction\":\"buy\",\"trade_status\":\"request\",\"trade_count\":\"100\",\"trade_price\":\"20.00\",\"cash_freezed\":\"2005.00\",\"stock_freezed\":\"0\",\"c_date\":\"2014-09-27 12:00:15:604\",\"c_time\":\"2014-09-27 15:59:55\",\"deal_count\":null,\"deal_price\":0}]}";

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null)
                            {
                                ret = qqcommon.code;

                                if (ret != "0")
                                {
                                    ret = string.Concat(qqcommon.code, ".", qqcommon.msg);
                                }

                                LogManager.TradeLogger.Info(string.Concat("qq:", qq, ".oid:", oid, ".code:", code,
                                    ".retcode:", qqcommon.code, ".retmsg:", qqcommon.msg));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret = ex.Message;

                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("撤单失败.", qq));
            }

            return ret;
        }

        /// <summary>
        /// 通过OpenID获取QQ号码
        /// </summary>
        public string GetUinByOpenId(string openid,string userid)
        {
            string key = string.Concat(QQConstDefine.OPENID_CACHED_KEY, ".", openid);

            string userqq = CacheManager.RedisCache.Get<string>(key);

            if (string.IsNullOrEmpty(userqq))
            {
                try
                {
                    string tid = StringHelper.GetRequestTID();

                    string yidata = "{\"openid\":\"" + openid + "\",\"requestid\":\"" + tid + "\"}";

                    yidata = EncryptionUtil.EncryptIt(yidata, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                    //string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/getUinByOpenId?yidata=", System.Web.HttpUtility.UrlEncode(yidata));
                    string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmUser/getUinByOpenId?yidata=", System.Web.HttpUtility.UrlEncode(yidata));

                    string result = WebApiHelper.getUrl(url);

                    if (!string.IsNullOrEmpty(result))
                    {
                        Result obj = JsonHelper.ToObject<Result>(result);

                        if (obj != null)
                        {
                            string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                            if (!string.IsNullOrEmpty(temp))
                            {
                                OpenIdQQ openIdQQ = JsonHelper.ToObject<OpenIdQQ>(temp);

                                if (openIdQQ != null && openIdQQ.code == "0" && openIdQQ.data != null && !string.IsNullOrEmpty(openIdQQ.data.qq))
                                {
                                    userqq = openIdQQ.data.qq;

                                    CacheManager.DefaultCache.Set<string>(key, userqq);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("通过OpenID获取QQ号码失败."));
                }
            }

            return userqq;
        }

        /// <summary>
        /// 获取股票五档行情
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetQuotesData(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return string.Empty;
            }

            try
            {
                return WebApiHelper.CallGetApi(string.Concat(ConfigHelper.QuotesDataUrl, code),Encoding.GetEncoding("gb2312"));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 是否已抽奖，非pc端报名返回已抽奖
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public bool IsLottery(string qq)
        {
            string result = CacheManager.QQCache.Get<string>(string.Concat(QQConstDefine.REG_CACHED_KEY, ":", qq));

            if (string.IsNullOrEmpty(result))   //未在爱炒股报名参赛
            {
                return true;
            }
            else
            {
                result = CacheManager.QQCache.Get<string>(string.Concat(QQConstDefine.LOTTERY_CACHED_KEY, ":", qq));

                if (!string.IsNullOrEmpty(result))  //已抽奖
                {
                    return true;
                }
                else
                {
                    return false;   //未抽奖
                }
                //TradeDal tradeDal = new TradeDal();

                //RegInfo regInfo = tradeDal.GetRegInfo(qq);

                //if (regInfo == null || regInfo.Lottery)    //非pc端报名 或 已抽奖
                //{
                //    return true;
                //}
                //else
                //{
                //    return false;
                //}
            }
        }

        /// <summary>
        /// 增加一次抽奖机会
        /// </summary>
        /// <param name="qq"></param>
        public void First(string qq)
        {
            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmLottery/first?yidata=", StringHelper.GetYiData(qq));

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            QQCommon qqcommon = JsonHelper.ToObject<QQCommon>(temp);

                            if (qqcommon != null)
                            {
                                LogManager.DefaultLogger.Debug("qq:", qq, ".code:", qqcommon.code, ".msg:", qqcommon.msg);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("增加一次抽奖机会失败.", qq));
            }
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public bool Lottery(string qq)
        {
            bool flag = false;

            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmLottery/lottery?yidata=", StringHelper.GetYiData(qq));

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);

                        if (!string.IsNullOrEmpty(temp))
                        {
                            LotteryObj lotteryObj = JsonHelper.ToObject<LotteryObj>(temp);

                            if (lotteryObj != null && lotteryObj.data != null && lotteryObj.data.result == true)
                            {
                                flag = true;
                            }

                            TradeDal tradeDal = new TradeDal();

                            tradeDal.UpdateRegInfo(new RegInfo() {
                                Abandon = false,
                                Lottery = true,
                                QQ = qq,
                                Win = flag
                            });

                            CacheManager.QQCache.Set<string>(string.Concat(QQConstDefine.LOTTERY_CACHED_KEY, ":", qq), qq);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("抽奖失败.", qq));
            }

            return flag;
        }

        /// <summary>
        /// 放弃抽奖
        /// </summary>
        /// <param name="qq"></param>
        public void AbandonLottery(string qq)
        {
            try
            {
                TradeDal tradeDal = new TradeDal();

                RegInfo regInfo = tradeDal.GetRegInfo(qq);

                if (regInfo != null)
                {
                    regInfo.Abandon = true;

                    tradeDal.UpdateRegInfo(regInfo);

                    CacheManager.QQCache.Get<string>(string.Concat(QQConstDefine.LOTTERY_CACHED_KEY, ":", qq));
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("放弃抽奖失败.", qq));
            }            
        }

        public void LotteryTest(string qq)
        {
            try
            {
                string url = string.Concat(ConfigHelper.QQUrl, "emoney2014/EmLottery/getLotteryNum?yidata=", StringHelper.GetYiData(qq));

                NetHelper net = new NetHelper();

                string result = WebApiHelper.getUrl(url);

                if (!string.IsNullOrEmpty(result))
                {
                    Result obj = JsonHelper.ToObject<Result>(result);

                    if (obj != null)
                    {
                        string temp = EncryptionUtil.DecryptIt(obj.result, ConfigHelper.AES_KEY, ConfigHelper.AES_IV);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
